$(document).ready(function() {  

	var cookieCleanScreen = "ccs";

	var cbodyOH = $("#cbody").height();
	var mainOH = $("#main").height();
	
	$("#fetch, #users, #selection, #options, #result").resize(function(e){
		var newCBH = cbodyOH + $("#fetch").height() + $("#users").height() + $("#selection").height() + $("#options").height() + $("#result").height()
		var newMH = mainOH + $("#fetch").height() + $("#users").height() + $("#selection").height() + $("#options").height() + $("#result").height()
		$("#cbody").height(newCBH+15);
		$("#main").height(newMH+15);
	});	
		
	var ajax_load = " <img src='../images/loading.gif' alt='loading...' />";  
	var loading_text;
	$("#startci").click(function(){
		$(this).remove();
		loading_text = "Fetching data from <a href=\"http://stackoverflow.com\" target=\"_blank\" title=\"Stack Overflow\">stackoverflow</a><br/>This will take approx. 15 secs"
		$("#fetch").html(loading_text + ajax_load).load("/start-ci",function(){
				$("#users").html("Processing users. Calculating scores" + ajax_load).load("/process-users",function(){
						$("#another-random").click(randomUserClick);
						$('#users-list').flexbox('user-json-list', {  
						    noResultsText: 'No such user', 
							width: 250,
//							allowInput: false,
							resultTemplate: '<span>{name} (</span><span>#Qs: {qs}, </span><span> #As: {as})</span>', //<div class="col1">{name}</div><div class="col2">#Qs: {qs}</div><div class="col3">#As: {as}</div>',  
						    watermark: "Type or Select user",   
						    onSelect: function() {  
								$("#selection").html("Getting user" + this.value + " details" + ajax_load).load("/get-user-details/" + this.getAttribute('hiddenValue'), function(){
									$(".show-tooltip").each(tooltip);
									$("#result").html("");
								})
						    }  
							});  
						
						$("#selection").html("Getting random user" + ajax_load).load("/random-user", randomUserSuccess);
				});	
		});
	});
	
	var optionsset = false;
	var opts = "<br/><input id=\"similar-user\" type=\"submit\" value=\"Get Similar User\"\/>" +
					"<input id=\"another-ci-alg\" type=\"submit\" disabled=\"disabled\" value=\"Apply another CI algorithm\"\/>";
	
	function randomUserSuccess(){
		$("#options").attr('disabled', '');
		$(".show-tooltip").each(tooltip);
		if (!optionsset){
			$("#options").html(opts);
			optionsset = true;	
			$("#similar-user").click(similarUserClick);
		}
			
//		$("#another-random").click(randomUserClick);
	}
	
	function similarUserClick(){
		var userId = $("#selection .username").attr("id");
		$("#result").html("Getting similar user" + ajax_load).load("/similar-user/" + userId, function(){
			$(".show-tooltip").each(tooltip);
		});
	}
	
	function randomUserClick(){
		$("#result").html("");
		$("#options").attr('disabled', 'disabled');
		//to enable - .attr('disabled', ''); 
		$("#selection").html("Getting another random user" + ajax_load).load("/random-user", randomUserSuccess);
	}
	
	function tooltip(){
		$(this).append($('<span/>')
		.attr('class', 'show-tooltip-text')
		.html($(this).attr('title')))
		.attr('title', '');
	}
	
	$(".show-tooltip").each(tooltip);
	
	(function($){
		// on every page load randomly shows or hides birdy
		var randomnumber = Math.floor(Math.random() * 101);
		if ((randomnumber % 2 ) == 0){
			// now you see me
			$("#birdy").addClass("yesBird");
		}else{
			// now you don't
			$("#birdy").addClass("noBird");
		}
		$(".side-menu-button p").text($("#side-menu-box > h4:first").text());
		
		// check if screen is clean
		if ($.cookie(cookieCleanScreen) != null){
			$("#hand").css("display",  "none");
			$("#side-menu-box dt#clean").next().text("It's clean!");	
		}
		
		// light box gallery
		$('#meme-gallery a.img').lightBox();
		
		equalHeight($(".equal_height"));
		
		$('#sidebar-box-fon a').each(function(){
			$(this).attr('href', function(index, attr){
				return "http://master.fon.rs/" + attr;
			})
		})
		
		$('#sidebar a').each(function(){
			$(this).attr('target', '_blank');
		})
	})(jQuery);

	//$("#birdy.yesBird #bird").mouseover(function(){alert('over')})
		
	$("#birdy").mouseover(function(){
		
	});
	
	//clean screen
	$("#side-menu-box dt#clean").click(function(){
		if ($.cookie(cookieCleanScreen) == null){
			$("#hand").animate({opacity: "hide"}, "slow");
			$.cookie(cookieCleanScreen, "It is clean!", {expires: 5});
			$(this).next().text("It's clean!");	
		}
	});
	
	
	//play game
	$("#side-menu-box dt#game a").click(function(){
		var s = document.createElement('script');
		s.type='text/javascript';
		document.body.appendChild(s);
		s.src='http://erkie.github.com/asteroids.min.js';
		void(0);
	})
	
	var csss;
	
	//nocss
	$("#side-menu-box dt#nocss a").click(function(){
		csss = new Array();
		$("link[type='text/css']").each(function(i){
			csss[i] = $(this).attr("href");
			$(this).remove();
		})
	});
	
	//addcss
	$("#side-menu-box dt#addcss a").click(function(){
		$.each(csss, function(i, l){
			var link = $("<link>");
			link.attr({
			        type: 'text/css',
			        rel: 'stylesheet',
			        href: l
			});
			$("head").append(link); 
		});
	});
	
	//slide side-menu show
	$("div.side-menu-button").click(function(event){
		event.stopPropagation();
		if (event.target.getAttribute("class") == "side-menu-button"){
			$("#side-menu-box").animate({
	    	left: '+=200px'
		  }, 1000, function() {});
		  $(this).animate({
	    	left: '+=200px'
		  }, 1000, function() {});
		  $(this).toggleClass("opened");
		}else
		{
			$("#side-menu-box").animate({
    	    left: '-=200px'
		    }, 1000, function() {});
		    $(this).animate({
	      		left: '-=200px'
		    }, 1000, function() {})
		    $(this).toggleClass("opened");
		}
		
	});
	
	$("div.photos, div.videos").mouseenter(function(){
		$(this).animate({top: "+=10px"}, "fast");
		
	});
	
	$("div.photos, div.videos").mouseleave(function(){
		$(this).animate({top: "-=10px"}, "meddium")
	});
	
	function equalHeight(group) {
		var tallest = 0;
		group.each(function() {
			var thisHeight = $(this).height();
			if(thisHeight > tallest) {
				tallest = thisHeight;
			}
		});
		group.height(tallest);
	}
	
	
	
});